package video

import (
	"context"
	"gorm.io/gorm"
	"srv/config"
	"srv/internal/model/dao"
	__ "srv/proto"
)

// 实现方法
func (s *UserService) VideoList(ctx context.Context, req *__.VideoListRequest) (resp *__.VideoListReply, err error) {
	var video []dao.Video
	config.DB.Where("video_name like ? or id=?", "%"+req.VideoName+"%", req.UserId).Scopes(Paginate(int(req.Page), int(req.PageSize))).Find(&video)
	var videos []*__.VideoList
	for _, d := range video {
		videos = append(videos, &__.VideoList{
			VideoName: d.VideoName,
			Url:       d.Url,
			UserId:    int64(d.UserId),
			Location:  d.Location,
			To:        int64(d.To),
		})
	}
	return &__.VideoListReply{List: videos}, nil
}
func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {

		if page <= 0 {
			page = 1
		}

		switch {
		case pageSize > 100:
			pageSize = 100
		case pageSize <= 0:
			pageSize = 10
		}

		offset := (page - 1) * pageSize
		return db.Offset(offset).Limit(pageSize)
	}
}
